name: Docker tests

on:
  workflow_call:
    inputs:
      command-json-array:
        required: true
        type: string

env:
  CACHE_FOLDER: ~/docker-images-${{ github.sha }}
  CACHE_FILE_UBUNTU_18_04: ~/docker-images-${{ github.sha }}/wal-g_ubuntu-18.04.tgz
  CACHE_FILE_UBUNTU_20_04: ~/docker-images-${{ github.sha }}/wal-g_ubuntu-20.04.tgz
  CACHE_FILE_GOLANG: ~/docker-images-${{ github.sha }}/wal-g_golang.tgz
  CACHE_FILE_DOCKER_PREFIX: ~/docker-images-${{ github.sha }}/docker_prefix.tgz
  IMAGE: wal-g/docker_prefix
  IMAGE_UBUNTU_18_04: wal-g/ubuntu:18.04
  IMAGE_UBUNTU_20_04: wal-g/ubuntu:20.04
  IMAGE_GOLANG: wal-g/golang
  IMAGES_CACHE_KEY: docker-images-${{ github.sha }}
  GO_VERSION: "1.20.5" # until this will be resolved https://github.com/golang/go/issues/61431

jobs:
  buildimages:
    name: Build images
    runs-on: ubuntu-20.04
    steps:
      #  use cache to pass docker images to the test jobs
      - name: Docker images caching
        id: cache-images
        uses: actions/cache@v4
        with:
          path: |
            ${{ env.CACHE_FILE_DOCKER_PREFIX }}
            ${{ env.CACHE_FILE_UBUNTU_18_04 }}
            ${{ env.CACHE_FILE_UBUNTU_20_04 }}
            ${{ env.CACHE_FILE_GOLANG }}
          key: ${{ env.IMAGES_CACHE_KEY }}

      - name: Install deps
        if: steps.cache-images.outputs.cache-hit != 'true'
        run: sudo apt-get install liblzo2-dev

      - name: Check out code into the Go module directory
        if: steps.cache-images.outputs.cache-hit != 'true'
        uses: actions/checkout@v4

      - name: Set up Go 1.x
        if: steps.cache-images.outputs.cache-hit != 'true'
        uses: actions/setup-go@v5
        with:
          go-version: ${{ env.GO_VERSION }}
        id: go

      - name: Get dependencies
        if: steps.cache-images.outputs.cache-hit != 'true'
        run: |
          go get -v -t -d ./...
          if [ -f Gopkg.toml ]; then
           curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
           dep ensure
          fi

      # build images
      - name: Build images
        if: steps.cache-images.outputs.cache-hit != 'true'
        run: make pg_save_image
    env:
      USE_BROTLI: 1
      USE_LZO: 1

  test:
    name: test
    runs-on: ubuntu-20.04
    needs: [ buildimages ]
    strategy:
      matrix:
        # GH Actions lack of complex types support for inputs.
        # Workaround https://github.com/orgs/community/discussions/11692#discussioncomment-3187271
        command: ${{ fromJSON(inputs.command-json-array) }}
      # do not cancel all tests if one failed
      fail-fast: false
    steps:
      - name: Install deps
        run: sudo apt-get install liblzo2-dev

      - name: Check out code into the Go module directory
        uses: actions/checkout@v4

      - name: Set up Go 1.x
        uses: actions/setup-go@v5
        with:
          go-version: ${{ env.GO_VERSION }}
        id: go

      - name: Get dependencies
        run: |
          go get -v -t -d ./...
          if [ -f Gopkg.toml ]; then
           curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
           dep ensure
          fi

      # load docker images
      - name: Load docker images
        id: cache-images
        uses: actions/cache@v4
        with:
          path: |
            ${{ env.CACHE_FILE_DOCKER_PREFIX }}
            ${{ env.CACHE_FILE_UBUNTU_18_04 }}
            ${{ env.CACHE_FILE_UBUNTU_20_04 }}
            ${{ env.CACHE_FILE_GOLANG }}
          key: ${{ env.IMAGES_CACHE_KEY }}

      - name: Fail if no cached images
        if: steps.cache-images.outputs.cache-hit != 'true'
        run: |
          echo "Failed to fetch cached docker images. Will now exit..."
          exit 1

      - name: Run test
        run: ${{ matrix.command }}
    env:
      USE_BROTLI: 1
      USE_LIBSODIUM: 1
      USE_LZO: 1
